<!--
 * @Description: 
 * @version: 
 * @Author: 171
 * @Date: 2022-09-01 22:36:56
 * @LastEditors: 171
 * @LastEditTime: 2022-09-22 17:55:29
-->
<script setup lang='ts'>

  import { onMounted, reactive, ref } from 'vue';
  import { useRouter } from 'vue-router';
  import { ElMessage, ElMessageBox } from 'element-plus'
  import avatar from '@/assets/灰原.jpg'
  import useStore from "../stores"

  const { userState } = useStore()
  
  const router = useRouter()

  // ----------------------------------------------------------------
  // 处理导航区的样式问题
  const navShow = ref<boolean>(false); 
  onMounted(() => {
    const container = document.getElementsByClassName('my-container')[0] as HTMLElement;
    container.addEventListener('mouseenter', () => {
      navShow.value = true;
    });
    container.addEventListener('mouseleave', () => {
      navShow.value = false;
    })
  })

  // ------------------------------------------------------------
  // 前往疫情路由
  const goToNav = (route: string) => { 
    router.push(`/${route}`)
  }

  // ----------------------------------------------------------------
  // 处理取消登录
  const cancelLogin = () => { 
  ElMessageBox.confirm(
    '您确定要执行此操作吗？',
    '退出登录',
    {
      confirmButtonText: '确定',
      cancelButtonText: '我再想想',
      type: 'info',
    }
  ).then(() => { 
    ElMessage({
      type: 'success',
      message: '注销成功，即将返回登录界面',
      duration: 1500,
      onClose: () => {
        localStorage.removeItem('token');
        localStorage.removeItem('id');
        localStorage.removeItem('publicKey');
        userState.updateToken();
        userState.updateUserInfo();
        router.push('/login');
      }
    })
  });
}

</script>
<script lang='ts'>
  
</script>

<template>

  <div class="my-container">
    <transition 
      enter-active-class="animate__animated animate__fadeInDown"
      leave-active-class="animate__animated animate__fadeOutUp"
    >
      <nav v-if="navShow">
        <section class="center">
          <div class="flex items-center" @click="goToNav('')">
            <div class="avatar">
              <el-avatar v-if="!userState.status" src="https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png"/> 
              <el-avatar v-else fit="fit" alt="None" :src="avatar" :size="45" />   
            </div>          
            <span class="ml-2 text-indigo-200">{{ userState.status ? '用户' : '游客' }}</span>   
          </div>
          <div class="nav-text" @click="goToNav('my')">个人</div>
          <div class="nav-text" @click="goToNav('talk')">说说</div>
          <div class="nav-text" @click="goToNav('interact')">聊天</div>
          <div class="nav-text" @click="goToNav('epidemic')">关注疫情</div>
        </section>
        <section v-if="userState.status" class="switch" @click="cancelLogin">
          <el-icon><SwitchButton /></el-icon>
          <span>退出登录</span>
        </section>
        <section v-else class="switch" @click="goToNav('login')">
          <el-icon><Ship /></el-icon>
          <span>点击登录</span>
        </section>
      </nav>
    </transition>
  </div>

</template>

<style scoped lang='less'>
.my-container{
  width: inherit;
  height: 8%;
  position: fixed;
  z-index: 10;
}
nav{
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.85);
  box-shadow: #fff 0 0 10px 0px ;//边框阴影
  display: flex;
  justify-content: center;
  align-items: center;
  .center{
    height: 100%;
    width: 50%;
    display: flex;
    justify-content: space-around;
    align-items: center;
    .nav-text:hover{
      color: #369df2;
      cursor: pointer;
    }
    .nav-text {
      transition: 0.5s;
      font-weight: 550;
      height: 100%;
      display: flex;
      align-items: center;
    }
  }
  .switch{
    position: absolute;
    cursor: pointer;
    right: 0;
    margin-right: 5%;
    display: flex;
    align-items: center;
    color: #a6a3a3;
  }
}
.avatar{
  transition: all 2.0s;
  cursor: pointer;
}
.avatar:hover{
  transform: rotate(360deg);
}
</style>